package com.facebook.drawee.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;

import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Supplier;
import com.facebook.drawee.interfaces.SimpleDraweeControllerBuilder;
import com.facebook.drawee.generic.GenericDraweeHierarchy;

import javax.annotation.Nullable;

public class SimpleDraweeView extends GenericDraweeView {
    private static Supplier<? extends SimpleDraweeControllerBuilder> sDraweeControllerBuilderSupplier;
    private SimpleDraweeControllerBuilder mSimpleDraweeControllerBuilder;

    public SimpleDraweeView(Context context) {
        super(context);
        init();
    }

    public SimpleDraweeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SimpleDraweeView(Context context, AttributeSet attrs, int defaultStyle) {
        super(context, attrs, defaultStyle);
        init();
    }

    @TargetApi(value = 21)
    public SimpleDraweeView(Context context, AttributeSet attrs, int defaultStyle, int arg4) {
        super(context, attrs, defaultStyle, arg4);
        init();
    }

    public SimpleDraweeView(Context context, GenericDraweeHierarchy genericDraweeHierarchy) {
        super(context, genericDraweeHierarchy);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Preconditions.checkNotNull(SimpleDraweeView.sDraweeControllerBuilderSupplier, "SimpleDraweeView was not initialized!");
            mSimpleDraweeControllerBuilder =  SimpleDraweeView.sDraweeControllerBuilderSupplier.get();
        }
    }

    public static void initialize(Supplier<? extends SimpleDraweeControllerBuilder> supplier) {
        SimpleDraweeView.sDraweeControllerBuilderSupplier = supplier;
    }

    public void setImageURI(Uri uri, @Nullable Object obj) {
        setController(mSimpleDraweeControllerBuilder.setCallerContext(obj)
                .setUri(uri)
                .setOldController(getController())
                .build());
    }

    public SimpleDraweeControllerBuilder getControllerBuilder() {
        return mSimpleDraweeControllerBuilder;
    }

    public void setImageURI(Uri uri) {
        setImageURI(uri, null);
    }
}

